# -*- coding:utf-8 -*-
"""
@FileName  :double_ma_strategy.py
@Time      :2022/7/5 10:42
@Author    :fsksf
"""
from vcat.core.template import BaseTemplate
from vcat.core.constant import (
    OrderType,
    DataLevel
)
from vcat.core.obj import (
    BarData,
    BarGenArrayParams,
    InitParams
)


class DoubleMAStrategy(BaseTemplate):

    author = 'kangyuqiang'
    brief = 'MACD策略'
    minuite1bar = BarGenArrayParams(bar_level=DataLevel.minute, bar_interval=1, arr_size=60)
    gen_params_list = [
        minuite1bar,
    ]

    init_params = {
        'fast_window': InitParams(key='fast_window', name='macd短周期', dtype=int, default=3),
        'slow_window': InitParams(key='slow_window', name='macd长周期', dtype=int, default=5)
    }
    display_params = []
    fast_window = 3
    slow_window = 5

    def __init__(self,
                 name: str,
                 contract_ids,
                 start_dt=None,
                 end_dt=None,
                 params=None
                 ):
        super(DoubleMAStrategy, self).__init__(
            name=name,
            contract_ids=contract_ids,
            start_dt=start_dt,
            end_dt=end_dt,
            params=params)

    def on_bar(self, bar: BarData):

        macd = self._bar_managers['minute1'].macd(fast_period=self.fast_window, slow_period=self.slow_window, signal_period=9)
        for symbol, ma in macd.items():
            diff, signal, hist = ma
            if diff > 0 and signal > 0:
                print('向上突破')
            elif diff < 0 and signal < 0:
                print('向下突破')